package com.qf.sy2103.ssm.Service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.sy2103.ssm.Dao.RouteDao;
import com.qf.sy2103.ssm.Service.RouteService;
import com.qf.sy2103.ssm.entity.Route;
import com.qf.sy2103.ssm.vo.Tablevo;
import com.qf.sy2103.ssm.vo.pagevo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RouteServiceImpl implements RouteService {
   @Autowired
    private RouteDao routeDao;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Tablevo<Route> getAll(pagevo pagevo, Route route) {
        PageHelper.startPage(pagevo.getPage(),pagevo.getLimit());
        List<Route> list = routeDao.getAll(route);
        PageInfo<Route> routePageInfo = new PageInfo<>(list);
        return new Tablevo<Route>(0,"ok",routePageInfo.getTotal(),list);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean addUse(Route route) {
        int i = routeDao.addUse(route);
        if(i==0){
            return false;
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean deleteUse(List<Integer> ids) {
       int i = routeDao.deleteUse(ids);
       if(i==0){
           return false;
       }
       return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean updateUse(Route route) {
       int i =  routeDao.updateUse(route);
       if(i==0){
           return false;
       }
        return true;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Tablevo<Route> getALL(pagevo pagevo) {
        PageHelper.startPage(pagevo.getPage(),pagevo.getLimit());
        List<Route> list = routeDao.getALL();
        PageInfo<Route> routePageInfo = new PageInfo<>(list);
        return new Tablevo<Route>(0,"ok",routePageInfo.getTotal(),list);
    }
}
